<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>获取用户信息</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        #container {
            max-width: 400px;
            margin: 0 auto;
            background: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #218838;
        }

        #result {
            margin-top: 20px;
            padding: 10px;
            background: #e9ecef;
            border-radius: 4px;
        }
    </style>
    <script src="./js/vue.js"></script>
</head>
<body>

    <div id="container">
        <h1>获取用户信息</h1>
        <input type="text" name="" v-model="username" id="name" placeholder="请输入用户名">
        <button @click="getUserInfo()">获取用户信息</button>
        <div id="result">{{username, password, id}}</div>
        <button @click="getUserAllInfo()">获取所有用户</button>
        <div id="resultAll"></div>
    </div>
   <!--  function getUserInfo() {

        var name = document.getElementById("username").value;
        var xhr = new XMLHttpRequest();
        xhr.open("GET", "http://localhost:8080/GetUserInfoServlet?username=" + name);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var data = JSON.parse(xhr.responseText);
                console.log(data);
                document.getElementById("result").innerHTML = 
                    "id: " + data.id + "<br>" + 
                    "用户名：" + data.username + "<br>" + 
                    "密码：" + data.password;
            }
        }
        xhr.send();
    } -->

    <script>
       new Vue({
            el:"#container",
            data: {
                username: "",
                password: "",
                id: "",
            },
            methods: {
                getUserInfo: function() {
                    var xhr = new XMLHttpRequest();
                    xhr.open("GET", "http://localhost:8080/GetUserInfoServlet?username=" + this.username);
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                var data = JSON.parse(xhr.responseText);
                console.log(data);
                document.getElementById("result").innerHTML = 
                this.username = data.username;
                console.log(this.username);
                this.password = data.password;
                this.id = data.id;
                document.getElementById("result").innerHTML = 
                                "id: " + data.id + "<br>" + 
                                "用户名：" + data.username + "<br>" + 
                                "密码：" + data.password;
                        }
            }
            xhr.send();
       },
          getUserAllInfo: function() {
                 var xhr = new XMLHttpRequest();
                 xhr.open("GET", "http://localhost:8080/GetAllUserInfoServlet");
                 xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var data = JSON.parse(xhr.responseText);
                    console.log(data);
                    var result = "";
                    for (var i = 0; i < data.length; i++) {
                        result += "id: " + data[i].id + "<br>" + 
                                  "用户名：" + data[i].username + "<br>" + 
                                  "密码：" + data[i].password + "<br><br>";
                    }
                    document.getElementById("resultAll").innerHTML = result;
                }
                }
                xhr.send();
             }
         }
    })
    
    /*     function getUserAllInfo() {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", "http://localhost:8080/GetAllUserInfoServlet");
            
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var data = JSON.parse(xhr.responseText);
                    console.log(data);
                    var result = "";
                    for (var i = 0; i < data.length; i++) {
                        result += "id: " + data[i].id + "<br>" + 
                                  "用户名：" + data[i].username + "<br>" + 
                                  "密码：" + data[i].password + "<br><br>";
                    }
                    document.getElementById("resultAll").innerHTML = result;
                }
                }
                xhr.send();
                }
 */
    </script>

</body>
</html>
